/**
 * @Auther: ayo
 * @Date: 2018/8/22 16:02
 * @Description: // TODO
 */
import { Injectable } from '@angular/core';

import { LoadingController } from '@ionic/angular';

@Injectable()
export class LoadingProvider {

    private load: any;

    constructor(public loadingCtrl: LoadingController) {
    }

    async show(message, type) {
        // duration: 5000
        this.load = this.loadingCtrl.create({
            spinner: null,
            mode: 'ios',
            message: `<ion-icon name="checkmark-circle" color="secondary"></ion-icon>` + message
        });
        setTimeout(() => {
            this.dismiss();
        }, 3000);
        await this.load.present();
    }

    /**
     * loading加载动画
     * http://ionicframework.com/docs/api/components/loading/LoadingController/
     * @param {string} op       // 取值：open hide
     * @param {string} message
     * @param {string} spinner    动画SVG  // 取值：ios ios-small bubbles circles crescent dots
     * @param {string} css
     * @param {boolean} showBackdrop    是否有黑色遮罩
     */
    loading(op: string, message: string = '', spinner: string = null, css: string = '', showBackdrop: boolean = true) {
        this.dismiss();
        if (op === 'hide') {
            this.dismiss();
        } else {
            // 是否有黑色遮罩
            // duration:3000
            this.load = this.loadingCtrl.create({
                spinner: null,
                message: message,
                cssClass: css,
                mode: 'ios',
                showBackdrop: showBackdrop
                // enableBackdropDismiss: false,
                // dismissOnPageChange: false
            });
            this.load.present();
            // setTimeout(() => {
            //     this.dismiss();
            // }, 3000);
        }
        // this.load.onDidDismiss(() => {
        //     console.log('Dismissed loading');
        // });
    }

    dismiss() {
        if (this.load) {
            this.load.dismiss();
            this.load = null;
        }
    }
}
